package com.yaozheng.vocationaltraining.service.impl;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IPoWoDictionaryView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.service.PoWoDictionaryService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

@EBean
public class PoWoDictionaryServiceImpl implements PoWoDictionaryService {
	IPoWoDictionaryView iTokenBaseView;

	@Override
	public void init(IPoWoDictionaryView iTokenBaseView) {
		this.iTokenBaseView = iTokenBaseView;
	}

	@Background
	@Override
	public void list(int lastId,int cateId,String keyWord,String sortBy,String sort) {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/dict/items?cateId="
							+ cateId +"&keyword="+keyWord+"&sortBy="+sortBy
							+"&sort="+sort+"&offset="+lastId, iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
						@Override
						public void responseSuccess(JSONObject jsonObject) {
							iTokenBaseView.responseSuccess(jsonObject);
						}

						@Override
						public void responseError(String error) {
							iTokenBaseView.responseError(error);
						}
					});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Background
	@Override
	public void loadCategories() {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/dict/categories",
							iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
								@Override
								public void responseSuccess(JSONObject jsonObject) {
									iTokenBaseView.categoriesListSuccess(jsonObject);
								}

								@Override
								public void responseError(String error) {
									iTokenBaseView.categoriesListError(error);
								}
							});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.categoriesListError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Override
	public void detail(Integer id) {
		try {
			OkHttpClientManager
					.getInstance()
					.get(Constants.ROOTURL + "/dict/item/"+id,
							iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
								@Override
								public void responseSuccess(JSONObject jsonObject) {
									iTokenBaseView.detailSuccess(jsonObject);
								}

								@Override
								public void responseError(String error) {
									iTokenBaseView.detailError(error);
								}
							});
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iTokenBaseView.isResponseResult()) {
				iTokenBaseView
						.detailError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

}
